package com.zrs.gmall.list.controller;

import com.zrs.gmall.common.result.Result;
import com.zrs.gmall.list.model.Goods;
import com.zrs.gmall.list.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@Api(tags = "ES上下架")
@RestController
@RequestMapping("api/list/inner")
@Slf4j
public class ListController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;


    /**
     * 创建索引库
     *
     * @return
     */
    @ApiOperation(value = "创建索引库")
    @GetMapping("/createIndex")
    public Result createIndex() {
        //调用工具类创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //设置索引库映射信息
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }


    /**
     * 删除索引库
     *
     * @return
     */
    @ApiOperation(value = "删除索引库")
    @GetMapping("/deleteIndex")
    public Result deleteIndex() {
        elasticsearchRestTemplate.deleteIndex("goods");
        return Result.ok();
    }


    /**
     * 仅仅为了测试:能将数据库中商品数据导入到索引库中 todo:将来采用基于MQ实现数据同步上下架
     *
     * @param skuId
     * @return
     */
    @ApiOperation(value = "仅仅为了测试:能将数据库中商品数据导入到索引库中")
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }


    /**
     * 仅为测试提供:删除索引库商品
     *
     * @param skuId
     * @return
     */
    @ApiOperation(value = "仅为测试提供:删除索引库商品")
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 开发阶段日志级别设置Info或者Debug 对接口入参信息,出参信息,重要业务方法结果
     * 生产环境日志设置为:Waring 或者 ERROR级别
     * @return
     */
    @ApiOperation(value = "日志测试")
    @GetMapping("/testerror")
    public Result testError() {
        log.debug("Debug级别的日志");
        log.info("Info级别的日志");
        log.warn("Warning级别的日志");
        log.error("Error级别的日志");
        return Result.ok();
    }
}